package com.wawayaya.bookreader.bean;

import android.text.TextUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;


/*  2014-09-23
The essential prerequisite for <KokRegion> data structure is:
1. 	There is NO sentence that spans two pages. Important!!!
2.  One <KokRegion> data structure is only for oner book page.
3.  There is no more than 31 sentences in one page. In such case, Map just list an Array. MAp structure is kept for possible non-integer map-key in future.
*/

public class KokRegion implements java.io.Serializable  {
	
	private static final long serialVersionUID = 1L;
	
	// no such function requirement now
	//private BTree<PosRegionKey, KokData> m_btKokRegions = new BTree<PosRegionKey, KokData>();
	//private Map<Integer, KokData> m_mapSentenceKokregions = Collections.synchronizedMap(new HashMap<Integer, KokData>());
	//private Map<String, BTree<TimeRangeKey, AudiofileTimeelapsedUnit> > m_mapAudiofileKokregions = Collections.synchronizedMap(new HashMap<String, BTree<TimeRangeKey, AudiofileTimeelapsedUnit> >());
	private Map<String, ArrayList<AudiofileTimeelapsedUnit> > m_mapAudiofileKokregions = Collections.synchronizedMap(new HashMap<String, ArrayList<AudiofileTimeelapsedUnit>>());
	private Map<String, IntDouble> m_mapAudiofileTimeBeginEnd = Collections.synchronizedMap(new HashMap<String, IntDouble>());
	private String m_saudiofilepathes = "%";
		
	public void add(int isentenceId, RectSerialIntDouble[] rsids, String ssudiopath, int iaudiobegin, int iaudioend) {
		if(isentenceId >= 0 && null != rsids && rsids.length > 0 && !TextUtils.isEmpty(ssudiopath)) {
			/* // no such function requirement now
			KokData kd = new KokData(isentenceId, ssudiopath, iaudiobegin, audioend, rsids);
			for(RectSerialIntDouble rsid : rsids) {
				if(null != rsid) {
					PosRegionKey rk = new PosRegionKey(rsid.m_rc); 
					m_btKokRegions.put(rk, kd);
				}
			}
			m_mapSentenceKokregions.put(isentenceId, kd);
			*/
			/* // no such function requirement now
			BTree<TimeRangeKey, AudiofileTimeelapsedUnit> bt = m_mapAudiofileKokregions.get(ssudiopath);
			if(null == bt){
				bt = new BTree<TimeRangeKey, AudiofileTimeelapsedUnit>();
			}
			for(RectSerialIntDouble rsid : rsids) {
				if(null != rsid && null != rsid.m_rc) {
					TimeRangeKey trk = new TimeRangeKey(rsid.m_beginend); 
					bt.put(trk, new AudiofileTimeelapsedUnit(rsid.m_rc, isentenceId, iaudiobegin, iaudioend));	
				}
			}
			m_mapAudiofileKokregions.put(ssudiopath, bt);
			*/
			ArrayList<AudiofileTimeelapsedUnit> altimeranges = m_mapAudiofileKokregions.get(ssudiopath);
			if(null == altimeranges) {
				altimeranges = new ArrayList<AudiofileTimeelapsedUnit>();
			}
			for(RectSerialIntDouble rsid : rsids) {
				if(null != rsid && null != rsid.m_rc) {
					altimeranges.add(new AudiofileTimeelapsedUnit(rsid.m_rc, isentenceId, rsid.m_beginend));
				}
			}
			m_mapAudiofileKokregions.put(ssudiopath, altimeranges);
			
			IntDouble idu = m_mapAudiofileTimeBeginEnd.get(ssudiopath);
			if(null == idu) {
				idu = new IntDouble(iaudiobegin, iaudioend);
			}
			else{
				idu.m_ival1 = Math.min(idu.m_ival1, iaudiobegin);
				idu.m_ival2 = Math.max(idu.m_ival2, iaudioend);
			}
			m_mapAudiofileTimeBeginEnd.put(ssudiopath, idu);
			
			if(m_saudiofilepathes.indexOf("%" + ssudiopath + "%") < 0) {
				m_saudiofilepathes += ssudiopath + "%";
			}
		}
	}

	// no such function requirement now
	/*public BTree<TimeRangeKey, AudiofileTimeelapsedUnit> getallkokelapsedtimerangebyaudiofiles(String saudiofilepath) {		
		return m_mapAudiofileKokregions.get(saudiofilepath);
	}*/
	
	public AudiofileTimeelapsedUnit[] getkokelapsedtimerangebyaudiofiles(String saudiofilepath) {
		ArrayList<AudiofileTimeelapsedUnit> altimeranges = m_mapAudiofileKokregions.get(saudiofilepath);
		if(null != altimeranges && altimeranges.size() > 0) {
			AudiofileTimeelapsedUnit[] units = new AudiofileTimeelapsedUnit[altimeranges.size()];
			for(int i = 0; i < altimeranges.size(); i++) {
				units[i] = altimeranges.get(i);
			}
			return units;
		}
		return null;
	}
	
	public IntDouble getaudiofileTimeRange(String saudiofilepath) {
		return m_mapAudiofileTimeBeginEnd.get(saudiofilepath);
	}
	
	public String[] getallaudiofilepathes() {
		int ilen = m_saudiofilepathes.length();
		if(ilen <= 2) { 
			return null;
		}
		String s = m_saudiofilepathes.substring(1, ilen - 1);
		return s.split("%");
	}
		
	/*// no such function requirement now
	public int getkokSentenceId(android.graphics.Point pt) {
		if(null == pt) {
			return -1;
		}
		PosRegionKey rk = new PosRegionKey(pt.x, pt.y, pt.x, pt.y);
		KokData kd = m_btKokRegions.getValue(rk);
		if(null != kd) {
			return kd.m_isentenceid;
		}
		return -1;
	}
	
	// no such function requirement now
	public String getkokaudiofile(android.graphics.Point pt) {
		if(null == pt) {
			return null;
		}
		PosRegionKey rk = new PosRegionKey(pt.x, pt.y, pt.x, pt.y);
		KokData kd = m_btKokRegions.getValue(rk);
		if(null != kd) {
			return kd.m_saudiopath;
		}
		return null;
	}*/
	
}
